from django.shortcuts import render, HttpResponse

from .models import Question
import os
# from django.conf import Settings  # 内置的settings
from first import settings


# Create your views here.

def index(request):
    return HttpResponse('llllllll')


def add_question(request):
    # 展示收集界面
    if request.method == "GET":
        return render(request, 'add_question.html')

    # 接收前台传递过来的数据  保存到数据库
    if request.method == "POST":
        title = request.POST.get('title')
        qtype = request.POST.get('qtype')
        option1 = request.POST.get('option1')
        option2 = request.POST.get('option2')
        option3 = request.POST.get('option3')
        option4 = request.POST.get('option4')
        ref_answer = request.POST.get('ref_answer')
        analysis = request.POST.get('analysis')

        q1 = Question(
            title=title,
            qtype=qtype,
            option1=option1,
            option2=option2,
            option3=option3,
            option4=option4,
            ref_answer=ref_answer,
            analysis=analysis
        )
        q1.save()
        result = "插入成功"
        return render(request, 'add_question.html', {"result": result})


def query_question(request):
    if request.method == "GET":
        questions = []
        return render(request, 'query_question.html', {"questions": questions})

    if request.method == "POST":
        wd = request.POST.get("wd")
        print(wd)

        # queryset中很多方法可以链式调用
        # questions = Question.objects.filter(title__contains='明显').filter(title__contains='客户')
        questions = Question.objects.filter(title__contains=wd)
        # questions = Question.objects.
        print(type(questions))
        print(questions)

        return render(request, 'query_question.html', {"questions": questions})


import os


def save_icon(iconfile):
    """
    将获取到的icon保存到本地
    :return: 图片的路径
    """
    # img_path = r'/static/img/'
    # img_path_all = img_path + iconfile.name
    # img_path_all2 = '..' + img_path + iconfile.name
    imgname = iconfile.name
    filename = os.path.join(settings.BASE_DIR, 'static/img/', imgname)
    print(filename)
    with open(filename, 'wb') as f:
        for chunk in iconfile.chunks():
            f.write(chunk)
    return '/static/img/' + imgname


from .models import Student


def exam(request):
    if request.method == "GET":
        return render(request, 'exam.html')

    name = request.POST.get("name", "")
    sex = request.POST.get("sex", "")
    cls = request.POST.get("cls", "")
    subject = request.POST.get("subject", "")
    icon = request.FILES.get("icon")

    # print(settings.BASE_DIR)
    img_path = save_icon(icon)
    print(img_path)
    s = Student(stu_name=name, sex=sex, cls=cls, subject=subject, img_path=img_path)
    s.save()

    return HttpResponse("一切ok!")


from .models import Score


def score(request):
    if request.method == "GET":
        return render(request, 'score.html')

    name = request.POST.get("name", "")
    blanks = request.POST.get("blanks", "")
    quers = request.POST.get("quers", "")
    comput = request.POST.get("comput", "")
    showok = request.POST.get("showok")

    s = Score(
        name=name,
        blanks=blanks,
        quers=quers,
        comput=comput,
        showok=showok,
    )

    s.save()
    return HttpResponse("ok")


from django.core.paginator import Paginator


# def stuList(request):
def stu_list(request):
    students = Student.objects.all()
    per_page = 3
    paginator = Paginator(students, per_page)
    max_page = paginator.num_pages
    # print(max_page,'--------')
    # page 页码 存在于url的参数中  路径?page=x123
    x = request.GET.get('page', 1)

    try:
        x = int(x)
        if x < 1:
            x = 1
        if x > max_page:
            x = max_page
    except:
        x = 1

    # pagex 是一个page对象, 不是list,也不能像list 容器一样使用
    pagex = paginator.get_page(x)
    # 第x页的学生
    students_pagex = pagex.object_list

    # if x == max_page:
    #     has_next = False
    # else:
    #     has_next = True
    # hasnot_next = x == max_page
    has_next = pagex.has_next()
    # return render(request, 'stu_list.html', {"students": students_pagex, 'current_page': x, 'max_page': max_page})
    # return render(request, 'stu_list.html', {"students": students_pagex, 'current_page': x, 'has_next': not hasnot_next})
    return render(request, 'stu_list.html', {"students": students_pagex, 'current_page': x, 'has_next': has_next})



def stu_del(request):
    stu_id = request.GET.get('id','')
    Student.objects.get(id=stu_id).delete()
    return HttpResponse("删除成功!")


def stu_get(request):
    stu_id = request.GET.get('id', '')
    stu = Student.objects.get(id=stu_id)

    return render(request,'stu_info.html',{"stu":stu})